// import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import router from './router';
import buildRoute from './router/buildRoute';
import { WHITE_LIST } from '@/router';
import { getToken, logout } from './utils/user';

// NProgress.configure({ showSpinner: false });

let dataFlag = false; // 标识：是否为第一次进入系统

router.beforeEach(async (to: any, from: any, next) => {
  // NProgress.start();
  // 白名单路由直接进入
  if (WHITE_LIST.includes(to.path)) {
    // NProgress.done();
    next();
    return true;
  }
  if (!dataFlag) {
    dataFlag = true;
    const accessToken = getToken() as string;
    if (accessToken) {
      // step 0
      await buildRoute();
      // await user.getUserProject()
      // NProgress.done();
    } else {
      logout();
      // NProgress.done();
      return false;
    }
    next({ ...to, replace: true });
  } else {
    // NProgress.done();
    next();
  }
});
